package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
@Service
@Transactional
public class UserServiceImp  implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Override
    public UserBean login(String user, String password) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_user",user);
        qw.eq("u_password",password);
        return userMapper.selectOne(qw);
    }

    @Override
    public ArrayList<UserBean> showAllUser() {
    return (ArrayList<UserBean>) userMapper.selectList(null);
    }

    @Override
    public boolean addUser(UserBean userBean) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_user",userBean.getUser());
        if( userMapper.selectOne(qw)==null){
            userMapper.insert(userBean);
            return true;
        }
        return false;
    }

    @Override
    public void delUser(Integer userId) {
        userMapper.deleteById(userId);
    }

    @Override
    public void updGradeById(Integer userId, String newGrade) {
        UserBean userBean = userMapper.selectById(userId);
        userBean.setGrade(newGrade);
        userMapper.updateById(userBean);
    }

    @Override
    public void updPasswordById(Integer userId, String newPassword) {
        UserBean userBean =userMapper.selectById(userId);
        userBean.setPassword(newPassword);
        userMapper.updateById(userBean);
    }

    @Override
    public UserBean userById(Integer userId) {
        return userMapper.selectById(userId);
    }
}
